package com.zhiyou.servlet.user;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhiyou.model.User;
import com.zhiyou.service.UserService;
import com.zhiyou.serviceImpl.UserServiceImpl;

/**
 * Servlet implementation class UserAddServlet
 */
@WebServlet("/UserAddServlet")
public class UserAddServlet extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String name = request.getParameter("u_name");
		String pass = request.getParameter("u_pass");
		String realname = request.getParameter("u_realname");
		String sex = request.getParameter("u_sex");
		User user = new User(name,pass,realname,sex);
		UserService us = new UserServiceImpl();
		boolean flag = us.add(user);
		if(flag){
			request.setAttribute("msg", "创建成功");
			request.getRequestDispatcher("/UserShowServlet?1=1").forward(request, response);
		}else{
			request.setAttribute("msg", "创建失败");
			request.getRequestDispatcher("view/user/add.jsp").forward(request, response);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
